package com.telecom.cms.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.ncr.teradata.bireport.InitParam;
import com.telecom.cms.dao.CampCustAppdRsltDAO;
import com.telecom.cms.dao.Dao;
import com.telecom.cms.dao.DaoImp;
import com.telecom.cms.dao.CampCustInfoDAO;
import com.telecom.cms.dao.CampCustAdvInfoDAO;
import com.telecom.cms.dto.CampCustInfo;
import com.telecom.cms.dto.CampCustInfoEx;
import com.telecom.cms.dto.CampCustAdvInfoEx;

import com.telecom.cms.util.Constant;

public class CampCustInfoMgr {
   
	HashMap campsqlmap=new  HashMap();
	public void save(CampCustInfo campcustbizinfo){
		(new CampCustInfoDAO()).insert(campcustbizinfo);
	}
	
	public void  delete(CampCustInfo campcustbizinfo){
		(new CampCustInfoDAO()).delete(campcustbizinfo);
	}
	
	public List findall(String campid,String campseqid,String custid,String prdinstid,String billingcycleid){
		List result=(new CampCustInfoDAO()).findAll(campid,campseqid,custid,prdinstid,billingcycleid);
		return result;
	}
	
	public List findallcycle(String campid,String campseqid){
		List result=(new CampCustInfoDAO()).findAllCycle(campid,campseqid);
		return result;
	}
	
	public HashMap  findallmap(String campid,String campseqid,List allidlist,String billingcycleid,int from)
	{
		HashMap result=(new CampCustInfoDAO()).findallmap( campid, campseqid, allidlist, billingcycleid,from);
		return result;
	}
	public List findalladv(String campid,String campseqid,String custid,String prdinstid,String billingcycleid){
		List result=(new CampCustAdvInfoDAO()).findAll(campid,campseqid,prdinstid,custid);
		return result;
	}
	
	public List findalladv2(String campid,String campseqid,String custid,String prdinstid,String billingcycleid,String userid){
		   CampCustAdvInfoEx p=null;
		   List result=new ArrayList();
		   ObjectInputStream ois=null;
		try {
			ois = new ObjectInputStream(
			           new FileInputStream("..\\webapps\\tcms\\cache\\"+campid+"_"+campseqid+"_"+userid+"_advinfo.txt"));
			 while((p=(CampCustAdvInfoEx)ois.readObject())!=null)
			 {
				// System.out.println("p is :"+p.getCustId()+custid+prdinstid+p.getCampAdv());
				 if(p.getCustId().trim().equals(custid.trim())&&p.getPrdInstId().trim().equals(prdinstid.trim())
						 &&null!=p.getCampAdv())
					 result.add(p.getCampAdv());
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			try {
				if(ois!=null)ois.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
          
           return result ;
	}
	
	public List findall2(String campid,String campseqid,String custid,String prdinstid,String billingcycleid,String userid)
		{
			   CampCustInfoEx p=null;
			   List result=new ArrayList();
			   ObjectInputStream ois=null;
			try {
				ois = new ObjectInputStream(
				           new FileInputStream("..\\webapps\\tcms\\cache\\"+campid+"_"+campseqid+"_"+userid+"_custinfo.txt"));
				 while((p=(CampCustInfoEx)ois.readObject())!=null)
				 {
					if(p.getCustid().trim().equals(custid.trim())&&p.getPrdinstid().trim().equals(prdinstid.trim())
							 &&campid.equals(""+p.getCampId())
							 &&(billingcycleid.trim().equals(p.getBillingCycleId().trim())||p.getBillingCycleId().trim().equals(""))
							 )
					{
						// System.out.println(p.getCustid()+p.getPrdinstid()+custid+p.getCampId()+campid+p.getBillingCycleId()+billingcycleid);
						 result.add(p);
					}
				 }
			} catch (Exception e) {
				// TODO Auto-generated catch block
				try {
					if(ois!=null)ois.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
	          
	           return result ;

		}
	
	public List findrealtimeinfo(String campid,String campseqid,String custid,String prdinstid,String billingcycleid){
		Dao dao = new DaoImp();
		String db=(new Constant()).getPara("DATABASE");
		List title=new ArrayList();
		List content=new ArrayList(); 
		List result=new ArrayList(); 
		
		ResultSet rs = null; 
		String reportConfFile = this.getClass().getResource("CampCustInfoMgr.class").getPath();
		reportConfFile =  reportConfFile.substring(0,reportConfFile.indexOf("WEB-INF")) + "WEB-INF/classes/"+InitParam.CAMP_SETTING_FILE;
		String line="";
		
		List campsqllist=null;
		
		try {
			if((campsqllist=(ArrayList)campsqlmap.get("camp"+campseqid))==null)
			{
				campsqllist=new ArrayList();
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				factory.setValidating(false);
				DocumentBuilder builder = factory.newDocumentBuilder();
				Document doc = builder.parse(new File(reportConfFile));

				NodeList reportList = doc.getElementsByTagName("camp"+campseqid);
				for(int i = 0;i < reportList.getLength();i++){
						line= reportList.item(i).getFirstChild().getNodeValue().trim();
						campsqllist.add(line);
				}
				
			}
			if(campsqllist!=null){
				for(int i=0;i<campsqllist.size();i++)
				{
					line=campsqllist.get(i).toString();
					line=line.replace("{camp_id}", campid);
					line=line.replace("{camp_seq_id}", campseqid);
					line=line.replace("{cust_id}", custid);
					line=line.replace("{prd_inst_id}", prdinstid);
					line=line.replace("{billing_cycle_id}", billingcycleid);
					
					 rs = dao.select(line);
					 int colCount = rs.getMetaData().getColumnCount();
					 while (rs.next()) {
							for(int j = 0; j < colCount; j++){
								title.add(rs.getMetaData().getColumnLabel(j + 1));
								content.add(rs.getObject(j + 1));
							}
					 }
				}
				campsqlmap.put("camp"+campseqid, campsqllist);
			}
			if (rs != null) {
				rs.close();
			}
			
			for(int i=0;i<title.size();i++){
				result.add(""+title.get(i)+":"+(i<content.size()?content.get(i).toString():""));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	public HashMap findalladvmap(String campid,String campseqid,List allidlist,String billingcycleid,int from){
		HashMap result=(new CampCustAdvInfoDAO()).findAllMap(campid,campseqid,allidlist,billingcycleid,from);
		return result;
	}
	
	public CampCustInfo findbyid(String campid,String campseqid,String custid,String prdinstid,String bizatomid){
		CampCustInfo result=(new CampCustInfoDAO()).findById(campid,campseqid,custid,prdinstid,bizatomid);
		return result;
	}
}
